<?php
namespace Isas\Controller;
use Think\Controller;
use Isas\Com\Indicator\Ma;

class DataHandleController extends AbstractIsasSupportController {
	
	public function index() {
		$this->display ( );
	}
	
	public function t_ma(){
		$ma = new Ma();
		$ma->tt();
	}
	
	public function ana_indicator(){
		$idc = I('idc');
		$year = I('year');
		$period = I('period');
		$idcPeriod = I('idc_period');
		$model = M('stock_'.$year.'_'.$period);
		if($idc=='ma'){
			$idcField = 'ma_'.$idcPeriod;
			// 获取所有代码
			$codes = $this->getAllCodes($model);
			for($n=0;$n<count($codes);$n++){
				$code = $codes[$n]['code'];
				$bars = $this->getIdcCalcBars($model, $code,$idcField);
				$ma = new Ma($idcPeriod,$model);
				$bars = $ma->calc($bars);
				$rst = $ma->save($bars);
			}
		}
		
	}
	
	private function addBars($model,$code,$bars,$lastBar,$barIndex,$lastDatetime){
		$type = 0;//0：上证，1：深证，2：创业板
		if($code>=600000) $type = 1;
		else if($code>=300000) $type = 2;
		else $type = 0;
		$dataList[] = array();
		$batchIndex = 0;
		for($n=0;$n<count($bars);$n++){
			if(!strpos($bars[$n],","))break;
			$info = explode(",",$bars[$n]);	
			$datetime = $info[0];
			$datetime = str_replace('/','-',$datetime);
			if(strtotime($datetime) <= $lastDatetime) continue;
			$_datetime = explode("-",$datetime);	
			$month = $_datetime[1];
			
			$bar[$n]['date_time'] = strtotime($datetime);
			$bar[$n]['year'] = $_datetime[0];
			$bar[$n]['month'] = $_datetime[1];
			$bar[$n]['day'] = $_datetime[2];
			$bar[$n]['code'] = $code;
			$bar[$n]['type'] = $type;
			$bar[$n]['open'] = $info[1];
			$bar[$n]['high'] = $info[2];
			$bar[$n]['low'] = $info[3];

			$bar[$n]['bar_index'] = $barIndex;
			$bar[$n]['close'] = $info[4];
			$bar[$n]['trade_volume'] = $info[5];
			$bar[$n]['trade_price'] = $info[6]; 

			if($n==0){
				if($lastBar[0]['close'] == null){
					// 第一个bar
					if($bar[0]['open'] == 0){
						$bar[0]['rise'] = 0;	
					} else {
						$bar[0]['rise'] = round((($bar[0]['close']-$bar[0]['open'])/$bar[0]['open'])*100,2);	
					}
				} else {
					if($lastBar[0]['open'] == 0){
						$bar[0]['rise'] = 0;
					} else {
						// 不是第一个bar
						$bar[0]['rise'] = round((($bar[0]['close']-$lastBar[0]['open'])/$lastBar[0]['open'])*100,2);	
					}
				}
			} else {
				// 计算第2个bar
				if($bar[$n-1]['open'] == 0){
					$bar[$n]['rise'] = 0;
				} else {
					$bar[$n]['rise'] = round((($bar[$n]['close']-$bar[$n-1]['close'])/$bar[$n-1]['close'])*100,2);
				}	
			}
			$barIndex++;
			$dataList[$batchIndex] = $bar[$n];
			if($batchIndex!=0 && $batchIndex%500 == 0){
				$model->addAll($dataList);
				$dataList = null;
				$dataList = array();
				$batchIndex = 0;
			} else {
				$batchIndex++;
			}
		}
		if(count($dataList)>0){
			$model->addAll($dataList);
			$dataList = null;
		}
	}
	
	/**
	 * 
	 */
	public function importStockData(){
		$path = I('filePath');
		$year = I('year');
		$filesnames = scandir($path);
		$model = M('stock_'.$year.'_1_d');
		$cntFiles = count($filesnames);
		for($i=2;$i<$cntFiles;$i++){
			$filePath = $path.'\\'.$filesnames[$i];
			$handle = fopen($filePath, "r");//读取二进制文件时，需要将第二个参数设置成'rb' 
			$content = fread($handle, filesize ($filePath));
			fclose($handle);
			if(strpos($content,"/")){
				$code = str_replace('.txt','',$filesnames[$i]);
				$lastBar = $model->field('open,high,low,close,bar_index,date_time')
					->where('code='.$code)->order('bar_index desc')->limit(1)->select();
				$barIndex = 0;
				$lastDatetime = 0;
				if($lastBar[0]['bar_index'] != null){
					$barIndex = ++$lastBar[0]['bar_index'];
					$lastDatetime = $lastBar[0]['date_time'];
				}
				$bars = explode("\r\n",$content);
				$this->addBars($model,$code,$bars,$lastBar,$barIndex,$lastDatetime);
			}
		}
	}
	
	
}